package com.itcaius.weblog.common.utils;

import com.itcaius.weblog.common.exception.BaseExceptionInterface;
import com.itcaius.weblog.common.exception.BizException;
import lombok.Data;

import java.io.Serializable;

/**
 * @author Caius
 * @description
 * @since Created in 2025-04-16
 */
@Data
public class Response<T> implements Serializable {

    //是否成功,默认为 true
    private boolean success = true;
    //响应信息
    private String message;
    //异常码
    private String errorCode;
    //响应数据
    private T data;

    //成功响应
    public static <T>  Response<T> success(T data){
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }
    public static <T> Response<T> success(){
        Response<T> response = new Response<>();
        return response;
    }

    //失败响应
    public static <T>  Response<T> fail(String errorCode,String errorMsg){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(errorCode);
        response.setMessage(errorMsg);
        return response;
    }
    public static <T> Response<T> fail(String errorMsg){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setMessage(errorMsg);
        return response;
    }
    public static <T> Response<T> fail(){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        return response;
    }
    //自定义返回的业务失败响应
    public static <T> Response<T> fail(BizException bizException){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }
    //其他类型的异常失败响应
    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface){
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setErrorCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }
}
